#include <cstdio>
#include <cstring>

int a[ 5001 ], f[ 1000000 ];
char s[ 5001 ];

int main( )
{
    int i, j, t, n, m, p;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d%d", &n, &m);
        for ( i = 0; i < n; i++ )
            scanf("%d", &a[ i ]);
        memset( f, 0, sizeof( f ) );
        for ( i = 0; i < m; i++ )
        {
            scanf("%s", s);
            p = 0;
            for ( j = 0; j < strlen( s ); j++ )
                if ( s[ j ] <= 'c' )
                    p = p * 10 + 2;
                else if ( s[ j ] <= 'f' )
                    p = p * 10 + 3;
                else if ( s[ j ] <= 'i' )
                    p = p * 10 + 4;
                else if ( s[ j ] <= 'l' )
                    p = p * 10 + 5;
                else if ( s[ j ] <= 'o' )
                    p = p * 10 + 6;
                else if ( s[ j ] <= 's' )
                    p = p * 10 + 7;
                else if ( s[ j ] <= 'v' )
                    p = p * 10 + 8;
                else if ( s[ j ] <= 'z' )
                    p = p * 10 + 9;
            f[ p ]++;
        }
        for ( i = 0; i < n; i++ )
            printf("%d\n", f[ a[ i ] ]);
    }
    return 0;
}
